package cn.itzzx.lagou.mybatis.pojo;

import cn.itzzx.lagou.mybatis.binding.MapperRegistry;
import cn.itzzx.lagou.mybatis.session.SqlSession;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 存放 sqlMapConfig.xml 配置文件的信息
 *
 * dataSource 对应的是 <dataSource/> 中的信息
 *
 * mappedStatementMap 一个mapper.xml 中的 select , insert ,update , delete 对应一个MappedStatement对象
 *
 * @author Martin
 * @date 2020-04-25
 * @see 1.0
 */
public class Configuration {

    private DataSource dataSource;

    private MapperRegistry mapperRegistry = new MapperRegistry();

    private Map<String,MappedStatement> mappedStatementMap = new HashMap<>();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }


    /**
     * 扫描该包下所有的 Mapper 接口，并添加到 mapperRegistry 中
     * @param packageName
     */
    public void addMappers(String packageName) {
        mapperRegistry.addMappers(packageName);
    }

    public <T> T getMapper(Class<?> type, SqlSession sqlSession){
        return this.mapperRegistry.getMapper(type,sqlSession);
    }


}
